package vn.co.thn.android.videohot.task;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.util.Log;

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.http.InputStreamContent;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoSnippet;
import com.google.api.services.youtube.model.VideoStatus;

public class VHUploadTask extends AsyncTask<String, Integer, Video> {
	static final String TAG = "VHUploadTask";
	private YouTube mYouTube;
	private UploadCallBack mUploadCallBack;
	private String mFilePath;

	public VHUploadTask(YouTube youTube, String FilePath, UploadCallBack uploadCallBack) {
		this.mYouTube = youTube;
		this.mUploadCallBack = uploadCallBack;
		this.mFilePath = FilePath;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		this.mUploadCallBack.start();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
		this.mUploadCallBack.inProcess(values[0]);
	}

	@Override
	protected Video doInBackground(String... params) {
		try {
			String VIDEO_FILE_FORMAT = "video/*";
			File videoFile = new File(mFilePath);
			Video videoObjectDefiningMetadata = new Video();
			VideoStatus status = new VideoStatus();
			status.setPrivacyStatus("public");
			videoObjectDefiningMetadata.setStatus(status);
			VideoSnippet snippet = new VideoSnippet();
			snippet.setTitle(params[0]);
			snippet.setDescription(params[1]);
			List<String> tags = new ArrayList<String>();
			tags.add(params[0]);
			snippet.setTags(tags);
			videoObjectDefiningMetadata.setSnippet(snippet);
			InputStreamContent mediaContent;
			mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, new BufferedInputStream(new FileInputStream(videoFile)));
			mediaContent.setLength(videoFile.length());
			YouTube.Videos.Insert videoInsert = mYouTube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);

			MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
			uploader.setDirectUploadEnabled(false);
			uploader.setChunkSize(1024 * 1024);
			MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
				public void progressChanged(MediaHttpUploader uploader) throws IOException {
					switch (uploader.getUploadState()) {
					case INITIATION_STARTED:
						Log.d(TAG, "Initiation Started");
						break;
					case INITIATION_COMPLETE:
						Log.d(TAG, "Initiation Completed");
						break;
					case MEDIA_IN_PROGRESS:
						Log.d(TAG, "Upload in progress");
						Log.d(TAG, "Upload percentage: " + uploader.getProgress());
						publishProgress((int) (uploader.getProgress() * 100));
						break;
					case MEDIA_COMPLETE:
						Log.d(TAG, "Upload Completed!");
						break;
					case NOT_STARTED:
						Log.d(TAG, "Upload Not Started!");
						break;
					}
				}
			};
			uploader.setProgressListener(progressListener);
			Video returnedVideo = videoInsert.execute();
			return returnedVideo;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	@Override
	protected void onPostExecute(Video result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		this.mUploadCallBack.conplete(result);
	}

	public interface UploadCallBack {
		public void start();

		public void inProcess(Integer update_process);

		public void conplete(Video video);
	}
}
